package com.study.services;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.ICusmshfitDAO;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.ICustomerconsolidationDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.IOrderformDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.CustomerconsolidationMapper;
import com.study.model.pojo.Cusmshift;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Customerconsolidation;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Orderform;
import com.study.model.pojo.User;

/**
 * 客户合并Service层
 * @author wjl
 *
 */
@Service
public class CustomerconsolidationServices {
	@Autowired
	ICustomerconsolidationDAO conlidation;
	@Autowired
	IUserDAO user;
	@Autowired
	ICustomerDAO cust;
	@Autowired
	IDictionaryDAO dict;
	@Autowired 
	CustomerconsolidationMapper consol;
	/**
	 * 添加客户合并记录 
	 *   * @param usersid  执行合并的操作人
	 * @param cusid   合并 的客户
	 * @param cusids2  被合并的客户id
	 * @return 
	 */
	public boolean addCusm(Integer usersid,String cusid,String cusids2){
		Customerconsolidation cusmshift=new Customerconsolidation();
		 User user3 = user.findById(usersid).get();
		 
		 //合并的客户
		// Customer customer = cust.findById(cusid).get();
		 Customer customer = cust.findByCusName(cusid);
	     Customer customer2 = cust.findByCusName(cusids2);
		 //被合并的客户
		 //Customer customer2 = cust.findById(cusids2).get();
		// Dictionary dictionary = (Dictionary) dict.findById(418).get();
		cusmshift.setUser3(customer.getUser());
		cusmshift.setUser1(user3);
		cusmshift.setUser2(customer2.getUser());
		cusmshift.setCustomer1(customer);
		cusmshift.setCustomer2(customer2);
		/*cusmshift.setCuctDate(new Date());*/
		cusmshift.setCuctDate(new Date());
		Customerconsolidation save = conlidation.save(cusmshift);
		 return  !save.equals("")?true:false;
	
	}
	
	/**
	 * 查询所有客户合并信息
	 * @return
	 */
	public List<JSONObject> getFindAll(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<JSONObject> selectCustomerconsolidionAll = consol.selectCustomerconsolidionAll();
		/*for (JSONObject jsonObject : selectCustomerconsolidionAll) {
			System.out.println(jsonObject);
		}*/
		return selectCustomerconsolidionAll;
	}
	
	
	/**
	 * 查询所有客户合并信息
	 * @return
	 */
	public List<JSONObject> getFindAllWhere(int pageNum, int pageSize, String name) {
		PageHelper.startPage(pageNum, pageSize);
		List<JSONObject> selectCustomerconsolidionAll = consol.selectCustomerloionWherse(name);
		
		return selectCustomerconsolidionAll;
	}
	
	
	
}
